package com.jz.beijianbao.legalPersonPlant.service.impl;

import com.jz.beijianbao.legalPersonPlant.bean.*;
import com.jz.beijianbao.legalPersonPlant.mapper.LegalPlantMapper;
import com.jz.beijianbao.legalPersonPlant.personEnum.PersonEnum;
import com.jz.beijianbao.legalPersonPlant.service.LegalPlantService;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LegalPlantServiceImpl implements LegalPlantService {

    @Autowired
    private LegalPlantMapper legalPlantMapper;


    //xiala
    @Override
    public ResultVO queryPerson(Integer id) {
        List<LegalPersonXiala> resultVO = legalPlantMapper.queryPerson(1);
        return ResultBuildVO.success(resultVO);
    }

    //分页
    @Override
    public ResultVO queryPag(PlantSelectVo plantSelectVo) {
        List<LegalPlant> legalPlantList = legalPlantMapper.queryPage(plantSelectVo);
        for (LegalPlant legalPlantVo : legalPlantList) {
            Integer enabled = legalPlantVo.getEnabled() ? 1:0;
            legalPlantVo.setEnabledName(PersonEnum.getName(enabled));
        }
        int count = legalPlantMapper.queryCount(plantSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setPageIndex(plantSelectVo.getPageIndex());
        allRecordsVo.setPageSize(plantSelectVo.getPageSize());
        allRecordsVo.countPage(count);
        allRecordsVo.setDataList(legalPlantList);
        return ResultBuildVO.success(allRecordsVo);

    }

    @Override
    public ResultVO add(LegalPlant legalPlant) {
        LegalPlant code = legalPlantMapper.queryByCode(legalPlant.getLegalPlantCode());
        if (code!=null){
            return ResultBuildVO.error("编码已经存在");
        }
        Date now = new Date();
        LegalPlant legalPlant1 = new LegalPlant();
        BeanUtils.copyProperties(legalPlant,legalPlant1);
        legalPlant1.setCreateName("从session中获取");
        legalPlant1.setCreateTime(now);
        legalPlantMapper.addPlant(legalPlant1);
        return ResultBuildVO.success();

    }

    @Override
    public ResultVO updatePlant(UpdatePlantVo updatePlantVo) {
        LegalPlant legalPlant = legalPlantMapper.queryById(updatePlantVo.getId());
        if (legalPlant==null){
            return ResultBuildVO.error("数据不存在");
        }
        if (legalPlant.getEnabled()){
            return ResultBuildVO.error("只有在禁用状态才能修改");
        }

        Date now = new Date();
        LegalPlant legalPlant1 = new LegalPlant();
        BeanUtils.copyProperties(updatePlantVo,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        legalPlantMapper.updatePlant(legalPlant1);
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO updateStatus(UpdateEnabledVo updateEnabledVo) {
        LegalPlant legalPlantVo1 = legalPlantMapper.queryEnabledById(updateEnabledVo.getId());
        if (legalPlantVo1==null) {
            return ResultBuildVO.error("数据不存在");
        }
        Date now = new Date();
        LegalPlant legalPlantVo = new LegalPlant();
        BeanUtils.copyProperties(updateEnabledVo,legalPlantVo);
        legalPlantVo.setUpdateName("从session获取");
        legalPlantVo.setUpdateTime(now);
        legalPlantMapper.updateStatus(legalPlantVo);
        return ResultBuildVO.success();
    }


}
